﻿using LSQL;
using Shopfloormanagement;
using System;
using System.Data;
using System.Windows.Forms;

namespace Shopfloormanagement
{
    public partial class Form_LPA_Member : DevExpress.XtraEditors.XtraForm
    {
        public string FormType = "Add";
        public int ID = -1;
        public Form_LPA_Member()
        {
            InitializeComponent();
        }
        public void LoadName()
        {
            //DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.sys, "Employees", "FactoryID=" + SystemInfo.readFactory + " and HomeDepartmentID=" + SystemInfo.readDepartment + " and Del_tag=0");
            DataTable dt1 = SQL.ReturnDataTable3(SystemInfo.SQL, "Employees", "FactoryID=" + SystemInfo.readFactory + " and Del_tag=0");
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                string displayName = dt1.Rows[i]["FullName"].ToString();
                string tagValue = dt1.Rows[i]["ID"].ToString();
                cmb_Name.Properties.Items.Add(new ComboBoxItem(displayName, tagValue));
            }
        }
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (cmb_Name.Text != "")
            {
                if (txt_Position.Text != "")
                {
                    if (FormType == "Add")
                    {
                        var selectedItem = cmb_Name.SelectedItem as ComboBoxItem;
                        if (selectedItem != null)
                        {
                            var tag = selectedItem.Tag; // 这里可以使用tag
                            if (SQL.ReturnRowsCount3(SystemInfo.SQL, "LPA_User", "AuditFactortyID=" + SystemInfo.readFactory + " and AuditDepartmentID=" + SystemInfo.readDepartment + " and EmployeeTitle='" + txt_Position.Text + "' and EmployeeID=" + tag + " and FullName='" + cmb_Name.Text + "' and Del_tag=0") == 0)
                            {
                                if (SQL.Insert(SystemInfo.SQL, "LPA_User", "AuditFactortyID,AuditDepartmentID,EmployeeTitle,EmployeeID,FullName", $"{SystemInfo.readFactory},{SystemInfo.readDepartment},'{txt_Position.Text}',{tag},'{cmb_Name.Text}'") > 0)
                                {
                                    cmb_Name.SelectedIndex = -1;
                                    txt_Position.Text = "";
                                    Form_LPA.frm.LoadMember();
                                }
                            }
                            else
                            {
                                DevExpress.XtraEditors.XtraMessageBox.Show("The content is repetitive!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    else if (FormType == "Revise")
                    {
                        if (ID != -1)
                        {
                            var selectedItem = cmb_Name.SelectedItem as ComboBoxItem;
                            if (selectedItem != null)
                            {
                                var tag = selectedItem.Tag; // 这里可以使用tag
                                string AuditFactortyID = "AuditFactortyID=" + SystemInfo.readFactory;
                                string AuditDepartmentID = "AuditDepartmentID=" + SystemInfo.readDepartment;
                                string EmployeeTitle = "EmployeeTitle='" + txt_Position.Text+"'";
                                string EmployeeID = "EmployeeID=" + tag;
                                string FullName = "FullName='" + cmb_Name.Text+"'";
                                if (SQL.Update(SystemInfo.SQL, "LPA_User", $"{AuditFactortyID},{AuditDepartmentID},{EmployeeTitle},{EmployeeID},{FullName}", "ID=" + ID) == true)
                                {
                                    this.Close();
                                    Form_LPA.frm.LoadMember();
                                }
                                else
                                {
                                    DevExpress.XtraEditors.XtraMessageBox.Show("Delete failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                        }
                    }
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Please enter position!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_Position.Focus();
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Please enter name!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            if (ID != -1)
            {
                if (SQL.Update(SystemInfo.SQL, "LPA_User", "Del_tag=1", "ID=" + ID) == true)
                {
                    this.Close();
                    Form_LPA.frm.LoadMember();
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("Delete failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void btn_Cancel2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public class ComboBoxItem
        {
            public string DisplayName { get; set; }
            public object Tag { get; set; }

            // 构造函数，接受两个参数
            public ComboBoxItem(string displayName, object tag)
            {
                DisplayName = displayName;
                Tag = tag;
            }

            public override string ToString()
            {
                return DisplayName; // 这将用于显示在ComboBox中
            }
        }
    }
}